package com.zf.yichat.controller.sys;


import com.alibaba.fastjson.JSON;
import com.zf.yichat.config.websocket.client.Client;
import com.zf.yichat.controller.BaseController;
import com.zf.yichat.model.SysSensitives;
import com.zf.yichat.service.SysSensitivesService;
import com.zf.yichat.service.config.SensitiveWordService;
import com.zf.yichat.utils.common.FsConst;
import com.zf.yichat.utils.response.FsPage;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Objects;

/**
 * 敏感词
 * @author xxx
 */
@Controller
@RequestMapping("/sensitives")
@Slf4j
public class SensitivesController extends BaseController {

    @Autowired
    private SysSensitivesService sysSensitivesService;

    @Autowired
    private SensitiveWordService sensitiveWordService;

    /**
     * 敏感词列表页面
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        return "sys/sensitives/index";
    }

    /**
     * 编辑or新增敏感词列表页面
     * @return
     */
    @RequestMapping("addOrEdit")
    public String addOrEdit(Integer id) {

        if (Objects.nonNull(id)) {
            // ModelAndView
            getRequest().setAttribute("sensitives", sysSensitivesService.selectSensitivesById(id));
        }

        return "sys/sensitives/edit";
    }

    /**
     * 敏感词列表
     * @param page
     * @param limit
     * @param word
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public FsResponse list(Integer page, Integer limit, String word) {
        return sysSensitivesService.selectList(FsPage.init(page, limit), word);
    }

    /**
     * 新增or更新敏感词
     * @param sensitives
     * @return
     */
    @RequestMapping("addOrEditData")
    @ResponseBody
    public FsResponse addOrEditData(SysSensitives sensitives) {

        try {
            String word = sensitives.getWord();
            String replaceWord = sensitives.getReplaceWord();
            if (StringUtils.isNotBlank(word)) {
                Integer code = sensitiveWordService.addSensitiveWord(word, replaceWord);
                // 表示添加敏感词成功
                if (FsConst.Status.IS_OK == code) {
                    return FsResponseGen.gen(sysSensitivesService.addOrEditSensitives(sensitives) == 1);
                }
            }
        } catch (Exception e) {
            log.info("获取敏感词服务失败：", e.getMessage());
        }

        return FsResponseGen.gen(false);
    }


    /**
     * 更新敏感词
     * @param sensitives
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public FsResponse updateSensitives(SysSensitives sensitives) {
        return FsResponseGen.gen(sysSensitivesService.updateSensitives(sensitives) == 1);
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public FsResponse deleteSensitive(Integer id) {
        try {
            SysSensitives sensitives = sysSensitivesService.selectSensitivesById(id);
            String word = sensitives.getWord();
            if (StringUtils.isNotBlank(word)) {
                Integer code = sensitiveWordService.deleteSensitiveWord(word);
                // 表示添加敏感词成功
                if (FsConst.Status.IS_OK == code) {
                    return FsResponseGen.gen(sysSensitivesService.deleteSensitive(id) == 1);
                }
            }
        } catch (Exception e) {
            log.info("删除敏感词服务失败：", e.getMessage());
        }
        return FsResponseGen.gen(false);
    }


}
